package com.yuma.tingshu.live.api;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yuma.tingshu.common.annotation.YuMaLogin;
import com.yuma.tingshu.common.result.Result;
import com.yuma.tingshu.common.util.AuthContextHolder;
import com.yuma.tingshu.live.service.LiveRoomService;
import com.yuma.tingshu.model.live.LiveRoom;
import com.yuma.tingshu.vo.live.LiveRoomVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("api/live/liveRoom")
@SuppressWarnings({"unchecked", "rawtypes"})
public class LiveRoomApiController {

    @Autowired
    private LiveRoomService liveRoomService;

    @YuMaLogin
    @GetMapping("getCurrentLive")
    public Result<LiveRoom> getCurrentLive() {
        LiveRoom liveRoom = this.liveRoomService.getOne(new LambdaQueryWrapper<LiveRoom>()
                .eq(LiveRoom::getUserId, AuthContextHolder.getUserId())
                .gt(LiveRoom::getExpireTime, new Date())
                .eq(LiveRoom::getStatus, 1));
        return Result.ok(liveRoom);
    }

    @YuMaLogin
    @PostMapping("saveLiveRoom")
    public Result<LiveRoom> saveLiveRoom(@RequestBody LiveRoomVo liveRoomVo) {
        LiveRoom liveRoom = this.liveRoomService.saveLiveRoom(liveRoomVo);
        return Result.ok(liveRoom);
    }

    @GetMapping("getById/{liveRoomId}")
    public Result<LiveRoom> getById(@PathVariable Long liveRoomId) {
        LiveRoom liveRoom = this.liveRoomService.getById(liveRoomId);
        return Result.ok(liveRoom);
    }

    @GetMapping("findLiveList")
    public Result<List<LiveRoom>> findLiveList() {
        List<LiveRoom> liveRooms = this.liveRoomService.list(new LambdaQueryWrapper<LiveRoom>()
                .gt(LiveRoom::getExpireTime, new Date())
                .eq(LiveRoom::getStatus, "1"));
        return Result.ok(liveRooms);
    }

}

